package com.inspurs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Champions {

    public static class Mapper_Champions extends Mapper<LongWritable, Text, Text, IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");
            String champion = split[2];

            Text newKey = new Text();

            String area = null;
            if(split.length == 7)
                area = split[6];

            if (area != null)
                newKey.set(champion+","+area);
            else
                newKey.set(champion);

            context.write(newKey, new IntWritable(1));
        }
    }

    public static class Reducer_Champions extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable iw : values) {
                sum += iw.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    public static class Partioner_Champion extends Partitioner<Text, IntWritable> {
        @Override
        public int getPartition(Text text, IntWritable intWritable, int numPartitions) {
            String[] split = text.toString().split(",");
            String area = null;
            if(split.length > 1)
                area = split[1];
            if("E".equals(area))
                return 0;
            else if("W".equals(area))
                return 1;
            else
                return 2;
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();

        //get job

        Job job = Job.getInstance(conf,"champions");

        //Set up the run main class for the job
        job.setJarByClass(Champions.class);

        //设置map阶段
        job.setMapperClass(Champions.Mapper_Champions.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://hadoop01:8020/0622/mapreduce_nba/clear_data/part-r-00000"));

        job.setReducerClass(Champions.Reducer_Champions.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop01:8020/0622/mapreduce_nba/champions"));

        job.setPartitionerClass(Champions.Partioner_Champion.class);
        job.setNumReduceTasks(3);

        int isok = job.waitForCompletion(true) ? 0 : 1;

        //退出job
        System.exit(isok);
    }
}